package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.AddNursingPlanDto;
import com.zzyl.dto.GetPageNursingsDto;
import com.zzyl.dto.UpdateNursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.entity.ProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Override
    public List<NursingPlanVo> getAllNursingPlan() {
      return   nursingPlanMapper.getAllNursingPlan();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNursingPlan(AddNursingPlanDto dto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto,nursingPlan);
        nursingPlanMapper.addNursingPlan(nursingPlan);
        List<ProjectPlan> projectPlans = dto.getProjectPlans();
        if (!projectPlans.isEmpty()){
            for (ProjectPlan projectPlan : projectPlans) {
                NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
                BeanUtils.copyProperties(projectPlan, nursingProjectPlan);
                nursingProjectPlan.setPlanId(nursingPlan.getId());
                nursingProjectPlan.setCreateTime(LocalDateTime.now());
                nursingProjectPlan.setUpdateTime(LocalDateTime.now());
                nursingPlanMapper.addNursingPlanAndProject(nursingProjectPlan);
            }
        }
    }
    @Override
    public PageResponse<NursingPlanVo> getPageNursingPlan(GetPageNursingsDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());
        List<NursingPlanVo> nursingPlanVoList= nursingPlanMapper.getPageNursingPlan(dto);
        Page<NursingPlanVo> nursingPlanVoList1 = (Page<NursingPlanVo>) nursingPlanVoList;
        PageResponse<NursingPlanVo> nursingPlanVoPageResponse = new PageResponse<>();
        nursingPlanVoPageResponse.setRecords(nursingPlanVoList1.getResult());
        nursingPlanVoPageResponse.setTotal(nursingPlanVoList1.getTotal());
        return nursingPlanVoPageResponse;
    }

    @Override
    public NursingPlanVo getNursingPlanById(Integer id) {

        NursingPlanVo nursingPlanVo= nursingPlanMapper.getNursingPlanById(id);
        List<NursingProjectPlanVo> nursingProjectPlanList = nursingPlanMapper.getnursingProjectAndPlanByPlanId(id);
        nursingPlanVo.setProjectPlans(nursingProjectPlanList);
        return nursingPlanVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteNursingPlanById(Integer id) {
        List<NursingProjectPlanVo> nursingProjectPlanList = nursingPlanMapper.getnursingProjectAndPlanByPlanId(id);
        if (!nursingProjectPlanList.isEmpty()){
            nursingPlanMapper.deleteNursingProjectPlanByPlanId(id);
        }
        nursingPlanMapper.deleteNursingPlanById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNursingPlanById(UpdateNursingPlanDto dto) {
        List<NursingProjectPlanVo> projectIds=nursingPlanMapper.getnursingProjectAndPlanByPlanId(dto.getId());
        if (!projectIds.isEmpty()){
            nursingPlanMapper.deleteNursingProjectPlanByPlanId(dto.getId());
        }
        NursingProjectPlan nursingProjectPlan1 = new NursingProjectPlan();
        BeanUtils.copyProperties(dto,nursingProjectPlan1);
        nursingPlanMapper.updateNursingById(dto);

        List<ProjectPlan> projectPlans = dto.getProjectPlans();
        if (!projectPlans.isEmpty()){
            for (ProjectPlan projectPlan : projectPlans) {
                NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
                BeanUtils.copyProperties(projectPlan, nursingProjectPlan);
                nursingProjectPlan.setPlanId(dto.getId());
                nursingProjectPlan.setCreateTime(LocalDateTime.now());
                nursingProjectPlan.setUpdateTime(LocalDateTime.now());
                nursingPlanMapper.addNursingPlanAndProject(nursingProjectPlan);
            }
        }
    }

    @Override
    public void updateStatus(Integer id, Integer status) {

        nursingPlanMapper.updateStatus(id,status);
    }
}
